package com.chen.seckill.service.impl;

import com.chen.seckill.exception.GlobalException;
import com.chen.seckill.pojo.LoginVO;
import com.chen.seckill.pojo.User;
import com.chen.seckill.mapper.UserMapper;
import com.chen.seckill.resultvo.RespBean;
import com.chen.seckill.resultvo.RespBeanEnum;
import com.chen.seckill.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.seckill.utils.CookieUtil;
import com.chen.seckill.utils.MD5Util;
import com.chen.seckill.utils.UUIDUtil;
import com.chen.seckill.utils.ValidatorUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenyuan
 * @since 2021-07-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    /**
     * 登录
     * @param loginVO
     */
    @Override
    public RespBean doLogin(LoginVO loginVO, HttpServletRequest request, HttpServletResponse response) {
        System.out.println(loginVO);
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
//        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password))
//        {
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
//        if (!ValidatorUtil.isMobile(mobile)){
//            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
//        }
        User user = userMapper.selectById(mobile);
        if(user==null){
            //1.返回RespBean对象
            // return RespBean.error(RespBeanEnum.LOGIN_ERROR);
            // 2.抛GlobalException异常 （两种方式都可以）
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        //判断密码是否正确
        if(!MD5Util.fromPasswdToDBPasswd(password,user.getSalt()).equals(user.getPassword())){
            //1.返回RespBean对象
            // return RespBean.error(RespBeanEnum.LOGIN_ERROR);
            // 2.抛GlobalException异常 （两种方式都可以）
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        //生成cookieID
        String ticket = UUIDUtil.uuid();
        String key="user:"+ticket;
        //将用户信息存入到redis中
        redisTemplate.opsForValue().set(key,user);
        //服务端
        //request.getSession().setAttribute(ticket,user);
        //客户端
        CookieUtil.setCookie(request,response,"userTicket",ticket);

        return RespBean.success(ticket);
    }

    /**
     * 根据cookie获取用户
     * @param userTicket
     * @return
     */
    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(userTicket)){
            return null;
        }
         String key="user:"+userTicket;
        User user = (User)redisTemplate.opsForValue().get(key);
        if (user!=null){
            //重新设置cookie
            CookieUtil.setCookie(request,response,"userTicket",userTicket);
        }
        return user;
    }
}
